package com.liushuai.eas.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liushuai.eas.common.Result;
import com.liushuai.eas.pojo.Classes;
import com.liushuai.eas.pojo.Course;
import com.liushuai.eas.pojo.Exam;
import com.liushuai.eas.service.ClassesImp;
import com.liushuai.eas.service.CourseImp;
import com.liushuai.eas.service.ExamImp;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/exam")
public class ExamController {

    @Resource
    private ExamImp examImp;

    @Resource
    private CourseImp courseImp;

    @Resource
    private ClassesImp classesImp;

    @PostMapping
    public Result<Exam> add(@RequestBody Exam exam){
        if (examImp.insert(exam)!=1)
            return Result.fail();
        return Result.success(exam);
    }

    @PutMapping
    public Result<Exam> update(@RequestBody Exam exam){
        if (examImp.updateById(exam)!=1)
            return Result.fail(exam);
        return Result.success(exam);
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        Page<Exam> examPage = examImp.selectPage(new Page<Exam>(pageNum, pageSize), Wrappers.<Exam>lambdaQuery().like(Exam::getYear, search));
        return Result.success("数据加载成功",examPage);
    }

    @DeleteMapping
    public Result<Exam> delete(@RequestBody Exam exam){
        if (examImp.deleteById(exam)!=1)
            return Result.fail(exam);
        return Result.success(exam);
    }

    @PostMapping(value = "/queryMyExam")
    public Result<Map<String, Object>> queryMyExam(@RequestBody Map<String,Integer> param) {
        //[{"course_id":"4","class_id":2},{"course_id":"4,8","class_id":3}]
        //获取学年
        Integer teacherId = param.get("teacherId");
        String year = getYear();
        HashMap<String, Object> resultMap = new HashMap<>();
        List<Map<String, String>> maps = examImp.queryClassCourseByTeacherId(teacherId, year);
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Map<String, String> map : maps){
            HashMap<String, Object> hashMap = new HashMap<>();
            Classes classes = classesImp.selectOne(Integer.parseInt(String.valueOf(map.get("class_id"))));
            hashMap.put("classes",classes);
            String[] courseIds = map.get("course_id").split(",");
            List<Course> courseList = new ArrayList<>();
            for (String courseId : courseIds){
                Course course = courseImp.selectOne(Integer.parseInt(courseId));
                courseList.add(course);
            }
            hashMap.put("courseList",courseList);
            mapList.add(hashMap);
        }
        resultMap.put("data",mapList);
        resultMap.put("year",year);
        return Result.success(resultMap);
    }

    //获取学年
    public String getYear() {
        Calendar calendar = Calendar.getInstance();
        String year = calendar.get(Calendar.YEAR)+"";
        try {
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            if (date.before(sdf.parse(year+"-06-30"))){
                year = year+"-1";
            }else {
                year = year+"-2";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return year;
    }

    @PostMapping("/queryExamByStudent")
    public Result<?> queryExamByStudent(@RequestBody Map<String,Integer> map){

        Integer studentId = map.get("studentId");
        List<Map<String,Object>> examList = examImp.queryExamByStudent(studentId);
        for (Map<String,Object> exam : examList){
            ArrayList<Exam> examArrayList = new ArrayList<>();
            String[] examIds = String.valueOf(exam.get("examId")).split(",");
            for (String id : examIds){
                Exam e = examImp.queryExamById(Integer.parseInt(id));
                Course course = courseImp.selectOne(e.getCourseId());
                e.setCourse(course);
                examArrayList.add(e);
            }
            exam.put("examList",examArrayList);
        }
        System.out.println(examList);
        return Result.success(examList);
    }

}
